﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
namespace WebTeamplate
{
    public partial class Admin_Default : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }
        public  string CreateCellBorder()
        {
            string result = string.Empty;
            int borderWidth = 1;
            string lineStyle = "Continuous";
            result += "<Borders>\r\n";
            result += "   <Border ss:Position='Bottom' ss:LineStyle='" + lineStyle + "' ss:Weight='" + borderWidth.ToString() + "'/>\r\n";
            result += "   <Border ss:Position='Left' ss:LineStyle='" + lineStyle + "' ss:Weight='" + borderWidth.ToString() + "'/>\r\n";
            result += "   <Border ss:Position='Right' ss:LineStyle='" + lineStyle + "' ss:Weight='" + borderWidth.ToString() + "'/>\r\n";
            result += "   <Border ss:Position='Top' ss:LineStyle='" + lineStyle + "' ss:Weight='" + borderWidth.ToString() + "'/>\r\n";
            result += "</Borders>\r\n";
            return result;
        }
        public  void ExportExcel(string strPathOut, string checkColum, string pageTitle)
        {

            try
            {
                //Export
                int columnCount = 4;// dgvSource.ColumnCount;
                string result = string.Empty;
                //Create page Infor
                result += "<?xml version='1.0' encoding='UTF-8'?>\r\n";
                result += "<?mso-application progid='Excel.Sheet'?>\r\n";
                result += "<Workbook xmlns='urn:schemas-microsoft-com:office:spreadsheet'\r\n";
                result += " xmlns:o='urn:schemas-microsoft-com:office:office'\r\n";
                result += " xmlns:x='urn:schemas-microsoft-com:office:excel'\r\n";
                result += " xmlns:ss='urn:schemas-microsoft-com:office:spreadsheet'\r\n";
                result += " xmlns:html='http://www.w3.org/TR/REC-html40'>\r\n";
                result += " <ExcelWorkbook xmlns='urn:schemas-microsoft-com:office:excel'></ExcelWorkbook>\r\n";
                //Create Page Style Sheet
                result += "   <Styles>\r\n";
                result += "       <Style ss:ID='PageTitle'>\r\n";
                result += "           <Alignment ss:Horizontal='Center' ss:Vertical='Center'/>\r\n";
                result += CreateCellBorder();
                result += "           <Font ss:FontName='Calibri' x:Family='Swiss' ss:Size='14' ss:Color='#000000'    ss:Bold='1'/>\r\n";
                result += "       </Style>\r\n";
                result += "       <Style ss:ID='pageHeader'>\r\n";
                result += "         <Alignment ss:Horizontal='Center' ss:Vertical='Center'/>\r\n";
                result += CreateCellBorder();
                result += "           <Font ss:FontName='Calibri' x:Family='Swiss' ss:Size='11' ss:Color='#000000' ss:Bold='1'/>\r\n";
                result += "         <Interior ss:Color='#BFBFBF' ss:Pattern='Solid'/>\r\n";
                result += "       </Style>\r\n";
                result += "      <Style ss:ID='data'>\r\n";
                result += CreateCellBorder();
                result += "       </Style>\r\n";
                result += "   </Styles>\r\n";
                //Create table  
                result += "   <Worksheet ss:Name='" + pageTitle + "'>\r\n";
                result += "       <Table  ss:DefaultRowHeight='15'>\r\n";
                result += "<Column ss:AutoFitWidth='0' ss:Width='100'/><Column ss:AutoFitWidth='0' ss:Width='100'/><Column ss:AutoFitWidth='0' ss:Width='100'/><Column ss:AutoFitWidth='0' ss:Width='100'/>";
                result += "           <Row ss:AutoFitHeight='0' ss:Height='19.5'>\r\n";
                result += "               <Cell ss:MergeAcross='" + (columnCount - 1).ToString() + "' ss:StyleID='PageTitle'><Data ss:Type='String'>" + pageTitle + "</Data></Cell>\r\n";
                result += "           </Row>\r\n";
                //Create Gird Header
                result += "           <Row ss:AutoFitHeight='0'>\r\n";
            
                        result += "     <Cell ss:StyleID='pageHeader'><Data ss:Type='String'>c.HeaderText</Data></Cell>\r\n";
                string  columnWidth = "  <Column ss:AutoFitWidth='0' ss:Width='100'/>\r\n";
                   
                result += "         </Row>\r\n";
                //Export Data                
                int rowCount = 3;
                for (int j = 0; j < rowCount; j++)
                {
                   
                        result += " <Row ss:AutoFitHeight='0'>\r\n";
                        for(int k=0;k<columnCount;k++)                            
                        {
                            string value = string.Empty;
                             value = "Content";
                                result += "<Cell ss:StyleID='data'><Data ss:Type='String'>" + value + "</Data></Cell>\r\n";
                           
                        }
                        result += " </Row>\r\n";
                   
                }
                result += "       </Table>";
                result += "   </Worksheet>";
                result += "</Workbook>";
                //result = string.Format(result, columnWidth);
                
                //StreamWriter strwriter = new StreamWriter(strPathOut);
                //strwriter.WriteLine(result.ToString());
                //strwriter.Close();
                Response.Cache.SetExpires(DateTime.Now.AddSeconds(1));
                Response.Clear();
                string strExten = ".xls";
                string strType = "application/vnd.ms-excel";
                //Response.AppendHeader("content-disposition", "attachment;filename=" + filename);
                Response.AppendHeader("content-disposition", "attachment;filename=testExcel.xls");
                Response.Charset = "UTF-8";
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                //Response.ContentType = "application/vnd.ms-excel";
                //Response.ContentType = "application/vnd.ms-word";
                Response.ContentType = strType;
                this.EnableViewState = false;
                Response.Write("\r\n");
                Response.Write(result);
                Response.End();
            }
            catch
            { }
        }
    }
}
